//
//  DesktopObserver.m
//  Do Not Disturb
//
//  Created by Chris Karr on 9/5/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "DesktopObserver.h"
#import "DNDDefines.h"

@implementation DesktopObserver

- (NSString *) getName
{
	return @"Desktop";
}

- (void) scan
{
	NSWorkspace * workspace = [NSWorkspace sharedWorkspace];

	NSDictionary * active = [workspace activeApplication];
	
	NSString * app = [active valueForKey:@"NSApplicationBundleIdentifier"];
	NSMutableDictionary * dict = [NSMutableDictionary dictionary];
	
	if (app != nil)
	{
		[dict setValue:[NSDate date] forKey:@"date"];
		[dict setValue:[NSString stringWithFormat:@"Active Application: %@ [%@]", [active valueForKey:@"NSApplicationName"], app] 
						forKey:@"name"];
		[dict setValue:active forKey:@"object"];
	
		[[NSDistributedNotificationCenter defaultCenter] postNotificationName:DND_UPDATE object:@"DesktopObserver" userInfo:dict];
	}

	NSArray * apps = [workspace launchedApplications];
	
	int i = 0;
	for (i = 0; i < [apps count]; i++)
	{
		NSDictionary * d = [apps objectAtIndex:i];
		
		NSString * identifier = [d valueForKey:@"NSApplicationBundleIdentifier"];
		NSString * name = [d valueForKey:@"NSApplicationName"];
		
		if (![app isEqual:identifier])
		{
			dict = [NSMutableDictionary dictionary];
			[dict setValue:[NSDate date] forKey:@"date"];
			[dict setValue:[NSString stringWithFormat:@"Inactive Application: %@ [%@]", name, identifier] forKey:@"name"];
			[dict setValue:d forKey:@"object"];
	
			[[NSDistributedNotificationCenter defaultCenter] postNotificationName:DND_UPDATE object:@"DesktopObserver" userInfo:dict];		
		}
	}
	
	NSArray * mounts = [workspace mountedLocalVolumePaths];
	
	for (i = 0; i < [mounts count]; i++)
	{
		NSString * path = [mounts objectAtIndex:i];

		dict = [NSMutableDictionary dictionary];
		[dict setValue:[NSDate date] forKey:@"date"];
		[dict setValue:[NSString stringWithFormat:@"Mounted Path: %@", path] forKey:@"name"];
	
		[[NSDistributedNotificationCenter defaultCenter] postNotificationName:DND_UPDATE object:@"DesktopObserver" userInfo:dict];		
	}
	
	NSAppleScript * script = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\"\n\
																		name of front window\n\
																	end tell"];

	NSAppleEventDescriptor * desc = [script executeAndReturnError:nil];
		
	if (desc != nil && [desc stringValue] != nil)
	{
		NSString * finderInfo = [desc stringValue];
				
		NSMutableDictionary * dict = [NSMutableDictionary dictionary];
		[dict setValue:[NSDate date] forKey:@"date"];
		[dict setValue:[NSString stringWithFormat:@"Frontmost Finder Window: %@", finderInfo] forKey:@"name"];
	
		[[NSDistributedNotificationCenter defaultCenter] postNotificationName:DND_UPDATE object:@"DesktopObserver" userInfo:dict];		
	}

	[script release];
	
	script = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\"\n\
																		name of windows\n\
																	end tell"];

	desc = [script executeAndReturnError:nil];
		
	for (i = 1; i < [desc numberOfItems] + 1; i++)
	{
		NSAppleEventDescriptor * listDesc = [desc descriptorAtIndex:i];

		if (listDesc != nil && [listDesc stringValue] != nil)
		{
			NSString * finderInfo = [listDesc stringValue];
				
			NSMutableDictionary * dict = [NSMutableDictionary dictionary];
			[dict setValue:[NSDate date] forKey:@"date"];
			[dict setValue:[NSString stringWithFormat:@"Open Finder Window: %@", finderInfo] forKey:@"name"];
	
			[[NSDistributedNotificationCenter defaultCenter] postNotificationName:DND_UPDATE object:@"DesktopObserver" userInfo:dict];		
		}
	}
	
	// [NSThread detachNewThreadSelector:@selector(fetchUnix:) toTarget:self withObject:nil];
}

- (void) fetchUnix:(id) obj
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	NSTask * task = [[NSTask alloc] init];
	[task setLaunchPath:@"/bin/ps"];
	[task setArguments:[NSArray arrayWithObjects:@"-o",  @"command", @"-xc", nil]];
	
	NSPipe * pipe = [NSPipe pipe];
	[task setStandardOutput:pipe];
	
	[task launch];
	
	[task waitUntilExit];
	
	NSFileHandle * outHandle = [pipe fileHandleForReading];
	
	NSString * pss = [[NSString alloc] initWithData:[outHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
	
	NSArray * psArray = [pss componentsSeparatedByString:@"\n"];

	int i = 0;
	for (i = 1; i < [psArray count]; i++)
	{
		NSMutableDictionary * dict = [NSMutableDictionary dictionary];
		[dict setValue:[NSDate date] forKey:@"date"];
		[dict setValue:[NSString stringWithFormat:@"Running Unix Process: %@", [psArray objectAtIndex:i]] forKey:@"name"];
	
		[[NSDistributedNotificationCenter defaultCenter] postNotificationName:DND_UPDATE object:@"DesktopObserver" userInfo:dict];		
	}
	
	[pss release];
	[task release];

	[pool release];
}

@end
